package com.yiyi.web.controller.system;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yiyi.common.core.redis.RedisCache;
import com.yiyi.web.controller.utils.IpAddressUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yiyi.common.annotation.Log;
import com.yiyi.common.core.controller.BaseController;
import com.yiyi.common.core.domain.AjaxResult;
import com.yiyi.common.enums.BusinessType;
import com.yiyi.system.domain.CsVisitorStatistics;
import com.yiyi.system.service.ICsVisitorStatisticsService;
import com.yiyi.common.utils.poi.ExcelUtil;
import com.yiyi.common.core.page.TableDataInfo;

/**
 * 访客统计Controller
 *
 * @author yiyi
 * @date 2024-10-11
 */
@RestController
@RequestMapping("/system/statistics")
public class CsVisitorStatisticsController extends BaseController
{
    @Autowired
    private ICsVisitorStatisticsService csVisitorStatisticsService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    public RedisTemplate redisTemplate;

    /**
     * 查询访客统计列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CsVisitorStatistics csVisitorStatistics)
    {
        startPage();
        List<CsVisitorStatistics> list = csVisitorStatisticsService.selectCsVisitorStatisticsList(csVisitorStatistics);
        return getDataTable(list);
    }

    /**
     * 导出访客统计列表
     */
    @Log(title = "访客统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsVisitorStatistics csVisitorStatistics)
    {
        List<CsVisitorStatistics> list = csVisitorStatisticsService.selectCsVisitorStatisticsList(csVisitorStatistics);
        ExcelUtil<CsVisitorStatistics> util = new ExcelUtil<CsVisitorStatistics>(CsVisitorStatistics.class);
        util.exportExcel(response, list, "访客统计数据");
    }

    /**
     * 获取访客统计详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(csVisitorStatisticsService.selectCsVisitorStatisticsById(id));
    }

    /**
     * 新增访客统计
     */
    @Log(title = "访客统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CsVisitorStatistics csVisitorStatistics, HttpServletRequest request)
    {
        // 获取请求的ip
        String requestIp = IpAddressUtil.getIpAddress(request);

        // 获取地址
        //String address = IpAddressUtil.getCityInfoByFile(requestIp);

        // 设置ip
        csVisitorStatistics.setUserIp(requestIp);
        // 设置地址
        csVisitorStatistics.setUserAddress(csVisitorStatistics.getUserAddress());
        // 设置时间
        csVisitorStatistics.setCreateTime(new Date());
        // 存储到 Redis
        String visitorKey = "visitor:logs";
        addVisitorStatistics(visitorKey, csVisitorStatistics);
       //csVisitorStatisticsService.insertCsVisitorStatistics(csVisitorStatistics);
        return success();
    }

    // 将单个对象存储到 Redis 列表中
    public void addVisitorStatistics(String key, CsVisitorStatistics statistics) {
        ListOperations<String, CsVisitorStatistics> listOps = redisTemplate.opsForList();
        listOps.rightPush(key, statistics);
    }


    /**
     * 修改访客统计
     */
    @Log(title = "访客统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsVisitorStatistics csVisitorStatistics)
    {
        return toAjax(csVisitorStatisticsService.updateCsVisitorStatistics(csVisitorStatistics));
    }

    /**
     * 根据订单编号修改支付状态
     */
    @RequestMapping("/updatePayStatusByOrderNum")
    public AjaxResult updatePayStatusByOrderNum(@RequestBody CsVisitorStatistics csVisitorStatistics){
        csVisitorStatistics.setPayStatus(1);
        csVisitorStatistics.setType("2");
        return toAjax(csVisitorStatisticsService.updatePayStatusByOrderNum(csVisitorStatistics));
    }

    /**
     * 删除访客统计
     */
    @Log(title = "访客统计", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(csVisitorStatisticsService.deleteCsVisitorStatisticsByIds(ids));
    }
}
